<?php
/**
 * User: hjd-yao
 * Date: 2019/2/22
 * Time: 16:22
 */

namespace App\Http\Controllers\Cms;


use App\Helpers\SelfResponse;
use App\Http\Controllers\BackendController;
use App\Models\Cms\NoticeModel;
use Illuminate\Http\Request;

class NoticeController extends BackendController
{
    private static $_noticeModel;

    public function __construct()
    {
        self::$_noticeModel = NoticeModel::init();
    }

    /**
     * @title 公告列表
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function index()
    {
        $list = self::$_noticeModel->paginate($this->getPerPageNum());
        return view('backend::cms.notice.notice_list',[
            'page_title'=>'公告列表',
            'list'=>$list
        ]);
    }

    /**
     * @title 发布公告
     * @param Request $request
     * @return SelfResponse|\Illuminate\View\View
     */
    public function create(Request $request)
    {
        if($request->isMethod('post')){
            self::$_noticeModel->title = $request->post('title');
            self::$_noticeModel->content = $request->post('content');
            self::$_noticeModel->type = $request->post('type',0);
            self::$_noticeModel->is_open = $request->post('is_open',1);
            if(self::$_noticeModel->save()){
                return SelfResponse::success('公告发布成功！','b_Notice_index');
            }
            return SelfResponse::error('公告发布失败,请重新尝试');
        }else{
            return view('backend::cms.notice.notice_add',[
               'page_title'=>'发布公告',
            ]);
        }
    }

    /**
     * @title 修改公告
     * @param Request $request
     * @param $id
     * @return SelfResponse|\Illuminate\View\View
     */
    public function update(Request $request,$id)
    {
        $info = self::$_noticeModel::find($id);
        if($request->isMethod('post')){
            $info->title = $request->post('title');
            $info->content = $request->post('content');
            $info->type = $request->post('type',0);
            $info->is_open = $request->post('is_open',1);
            if($info->save()){
                return SelfResponse::success('修改公告成功！','b_Notice_index');
            }
            return SelfResponse::error('公告修改失败,请重新尝试');
        }else{
            return view('backend::cms.notice.notice_edit',[
                'page_title'=>'编辑公告',
                'info'=>$info
            ]);
        }
    }

    /**
     * @title 发布/关闭公告
     * @param $id
     * @return SelfResponse|\Illuminate\Http\JsonResponse|\Illuminate\Http\RedirectResponse
     */
    public function switchNotice($id)
    {
        $info = self::$_noticeModel->find($id);
        if(!$info){
            return SelfResponse::error('修改信息不存在！');
        }
        $info->is_open = abs($info->is_open-1);
        if(!$info->save()){
            return SelfResponse::error('更新失败');
        }
        return SelfResponse::success('更新成功');
    }

    /**
     * @title 删除公告
     * @param $id
     * @return SelfResponse|\Illuminate\Http\JsonResponse|\Illuminate\Http\RedirectResponse
     */
    public function remove($id)
    {
        $info = self::$_noticeModel->find($id);

        if(!$info){
            return SelfResponse::error('修改信息不存在！');
        }

        if(!self::$_noticeModel->where('id',$id)->delete()){
            return SelfResponse::error('删除失败！');
        }

        return SelfResponse::success('删除成功！','b_Notice_index');
    }
}